package com.hn.shopadmin.controller;

import com.github.pagehelper.PageInfo;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopcommon.result.CommonPage;
import com.hn.kiss.modules.sys.shopdb.entity.DictDetail;
import com.hn.shopadmin.service.DictDetailService;
import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "DictDetailController", description = "字典明细管理")
@RestController
@RequestMapping("/dictDetail")
@Validated
public class DictDetailController {
    @Autowired
    DictDetailService dictDetailService;

    @ApiOperation("字典明细添加")
    @PostMapping("/add")
    public CommonResult add(DictDetail dictDetail) throws ParmException {
        //代码转化为大写
        dictDetail.setTypeCode(dictDetail.getTypeCode().toUpperCase());
        dictDetail.setDictCode(dictDetail.getDictCode().toUpperCase());

        int count = dictDetailService.add(dictDetail);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("字典明细修改")
    @PutMapping("/update")
    public CommonResult update(DictDetail dictDetail) throws ParmException {
        //代码转化为大写
        dictDetail.setTypeCode(dictDetail.getTypeCode().toUpperCase());
        dictDetail.setDictCode(dictDetail.getDictCode().toUpperCase());

        int count = dictDetailService.update(dictDetail);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("字典启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeCode", value = "类别代码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dictCode", value = "字典代码", required = true, dataType = "String", paramType = "query")
    })
    @PutMapping("/start")
    public CommonResult start(@NotBlank(message = "类别不能为空") String typeCode,
                              @NotBlank(message = "字典代码不能为空") String dictCode){
        int count = dictDetailService.start(typeCode, dictCode);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("字典停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeCode", value = "类别代码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dictCode", value = "字典代码", required = true, dataType = "String", paramType = "query")
    })
    @PutMapping("/stop")
    public CommonResult stop(@NotBlank(message = "类别不能为空") String typeCode,
                              @NotBlank(message = "字典代码不能为空") String dictCode){
        int count = dictDetailService.stop(typeCode, dictCode);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("字典明细分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "typeCode", value = "字典类别代码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1启用,0停用)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "keywork", value = "字典名称/字典代码", dataType = "String", paramType = "query")
    })
    @GetMapping("/listPage")
    public CommonPage<DictDetail> listPage(@NotNull(message = "page不能为空") Integer page,
                                           @NotNull(message = "page不能为空") Integer limit,
                                           String typeCode,
                                           Integer state,
                                           String keywork){
        PageInfo<DictDetail> dictDetailPageInfo = dictDetailService.listPage(page, limit, typeCode, keywork, state);
        return CommonPage.restPage(dictDetailPageInfo);
    }

    @ApiOperation("字典列表")
    @ApiImplicitParam(name = "typeCode", value = "类别代码", required = true, dataType = "String", paramType = "query")
    @GetMapping("/listAll")
    public CommonResult<List<DictDetail>> listAll(@NotBlank(message = "类别代码不能为空") String typeCode){
        List<DictDetail> dictDetails = dictDetailService.listAll(typeCode);
        return CommonResult.success(dictDetails);
    }

}
